package com.lanhai.system;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lanhai.Main;
import com.lanhai.model.Job;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;


/**
 * 删除数据操作
 * @author liangmin
 *
 */
public class DeleteHandler implements HttpHandler {
	
	public Main main;
	
    @Override
    public void handle(HttpExchange httpExchange) {
    	try{
            InputStream is = httpExchange.getRequestBody();
            
            String requestData = is2string(is);
            //String queryParams = httpExchange.getRequestURI().getQuery();
            String response = "";
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("success", false);
            //业务处理
            JSONObject params = JSONObject.parseObject(new String(requestData));
            String obj = params.getString("obj");
            if(obj != null){
                if(obj.equals("job")){//删除任务
                	List<Job> newJobList = new ArrayList<Job>();
                	String line = params.getString("line");
                	int step = params.getInteger("step");
                	for(Job j:main.jobList){
                		if(!(j.getLine().equals(line) && j.getStep() == step)){//根据线别删除
                			newJobList.add(j);
                		}
                	}
                	main.jobList = newJobList;
                	map.put("success", true);
                }            	
            }
            response = JSON.toJSONString(map);
            System.out.println("response: " + response);        
            is.close();
            
            //返回数据
            Headers responseHeaders = httpExchange.getResponseHeaders();
            responseHeaders.set("Content-Type", "text/json;charset=utf-8");
            httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.getBytes().length); 
            OutputStream os = httpExchange.getResponseBody();
            os.write(response.getBytes());
            os.flush();
            os.close();      		
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
	
    public void init(Main main){
    	this.main = main;
    }
    
    private String is2string(InputStream is) throws IOException {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(is, "UTF-8");
        
        for (; ; ) {
            int rsz = in.read(buffer, 0, buffer.length);
            if (rsz < 0)
                break;
            out.append(buffer, 0, rsz);
        }
        return out.toString();
    }
    
}
